<!-- 图片上传组件（单个），封装了预览和删除 -->
<template>
  <div class="attestation-uploader">
    <el-upload
      :disabled="!!imageUrl"
      :show-file-list="false"
      class="avatar-uploader"
      :accept="accept"
      action="https://jsonplaceholder.typicode.com/posts/"
      list-type="picture-card"
      :headers="headers"
      :before-upload="beforeAvatarUpload"
      :on-success="handleAvatarSuccess">
      <!-- 有图片 -->
      <template v-if="imageUrl">
        <img :src="imageUrl" class="avatar">
        <div class="attestation-icon-container">
          <div class="attestation-icon">
            <i @click="handlePictureCardPreview" title="预览" class="el-icon-view" />
            <i @click="handleRemove" title="删除" class="el-icon-delete" />
          </div>
        </div>
      </template>
      <!-- 默认图片 -->
      <template v-else>
        <span class="attestation-title" v-if="title">{{title}}</span>
        <i class="el-icon-plus" v-else></i>
      </template>
    </el-upload>
    <el-dialog append-to-body :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "attestationUploadImage",
  props: {
    title: {
      type: String,
      default: ''
    },
    accept: {
      type: String,
      default: '.png, .jpg, .jpeg'
    },
    size: {
      type: Number,
      default: 10
    },
  },
  data() {
    return {
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      headers: { Authorization: 'Bearer ' + getToken() },
      acceptList: this.accept.split('.').join('').split(' ').join('').split(','),
    };
  },
  created() {
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res, file)
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = this.acceptList.includes(file.name.substring(file.name.lastIndexOf(".")+1))
      const isLt2M = file.size / 1024 / 1024 < this.size;

      if (!isJPG) {
        this.$message.error(`上传图片只能是${this.acceptList.join("/")}格式`);
      }
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过${this.size}MB`);
      }
      return isJPG && isLt2M;
    },
    handlePictureCardPreview() {
      this.dialogImageUrl = this.imageUrl;
      this.dialogVisible = true;
    },
    handleRemove() {
      console.log();
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.attestation-uploader {
  width: 90px;
  height: 90px;
  display: inline-block;
  .avatar-uploader .el-upload {
    border-radius: 6px;
    cursor: pointer;
    position: relative;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
  .el-upload--picture-card .el-icon-plus {
    margin-top: calc(50% - 14px);
  }
  .attestation-title {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: auto;
    width: 100%;
    color: #333333;
    transform: translate(-50%,-50%);
  }
  .attestation-icon-container {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 100%;
    color: #333333;
    transition: opacity .3s;
    opacity: 0;
    background-color: rgba(0,0,0,.5);
    &:hover {
      opacity: 1;
    }
  }
  .attestation-icon {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: auto;
    width: 100%;
    color: #333333;
    transform: translate(-50%,-50%);
    i {
      font-size: 20px;
      color: #fff;
    }
    i:not(:last-child){
      margin-right: 15px;
    }
  }
  .el-upload--picture-card {
    width: 92px;
    height: 92px;
    line-height: 16px;
    position: relative;
  }
}
</style>
